home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / dial / d_numbers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-04  |  6.4 KB  |  292 lines

  1. # include  "util.h"
  2. # include  "d_returns.h"
  3. # include  "d_proto.h"
  4. # include  <stdio.h>
  5. # include  "d_structs.h"
  6.  
  7. /*
  8.  *     D_NUMPARSE
  9.  *
  10.  *     this routine is called to parse a phone number specification string
  11.  *     of the form:
  12.  *
  13.  *     speed|phone number, speed|phone number, ...
  14.  *
  15.  *     such as:
  16.  *
  17.  *     300|9:738-8003,1200|9:738-2928
  18.  *
  19.  *     and fill a table of structures with the appropriate information.
  20.  *     the elements of each table entry contain the speed index corresponding
  21.  *     to the speed designation (300, 1200, etc.) and the phone number is
  22.  *     converted from the external form into that required by the acu driver.
  23.  *
  24.  *     string - pointer to specification string as above
  25.  *
  26.  *     numtab - pointer to array of structures for the speed index, phone
  27.  *              number pairs
  28.  *
  29.  *     filename - name of the file from which the specification string came.
  30.  *                this name is reported when errors are encountered to aid
  31.  *                in debugging.
  32.  *
  33.  *     lineno - number of the line in the above file which contains the
  34.  *              string
  35.  *
  36.  *
  37.  *      Mar 84 D. Long  added support for "<type>" in phone numbers 
  38.  */
  39.  
  40. d_numparse(string, numtab, maxnums, filename, lineno)
  41.   char  *string, *filename;
  42.   struct telspeed  *numtab;
  43.   int  maxnums;
  44.   unsigned lineno;
  45.     {
  46.     register char  *in, *ap, *cp;
  47.     int  count;
  48.     char  accum[30], tempbuf[50];
  49.     int  index, num, linetype;
  50.  
  51.     in = string;
  52.     num = 0;
  53. #ifdef D_DBGLOG
  54.     d_dbglog("d_numparse", "parsing '%s'", string);
  55. #endif D_DBGLOG
  56.  
  57. /*  treat each speed/phone number pair separately  */
  58.  
  59.     while (1)
  60.       {
  61.  
  62. /*  isolate a number and copy it into a temporary buffer  */
  63.  
  64.       if (*in == ',')
  65.         in++;
  66.  
  67.       if (*in == '\0')
  68.         return(num);
  69.  
  70.       if (num >= maxnums)
  71.       {
  72.       d_fillog(filename, lineno, "d_numparse",
  73.             "more than %d phone numbers", maxnums);
  74.       return(D_FATAL);
  75.       }
  76.  
  77.       cp = tempbuf;
  78.       count = 0;
  79.  
  80.       while (*in && (*in != ','))
  81.     if (++count > 50)
  82.           {
  83.           d_fillog(filename, lineno, "d_numparse", "phone number too long");
  84.           return(D_FATAL);
  85.           }
  86.  
  87.         else
  88.           *cp++ = *in++;
  89.  
  90.       *cp = '\0';
  91.       cp = tempbuf;
  92.  
  93. /*  if the string begins with a letter, then it is taken to be the name of  */
  94. /*  a direct connect line.  the speed index will be set to 0.               */
  95.  
  96.       if (isalpha(tempbuf[0]))
  97.         {
  98.         if (strlen(tempbuf) > 25)
  99.           {
  100.           d_fillog(filename, lineno, "d_numparse", "direct line name too long");
  101.           return(D_FATAL);
  102.           }
  103.  
  104.         (void) strcpy(numtab[num].t_number, tempbuf);
  105.         numtab[num].t_speed = 0;
  106. #ifdef D_DBGLOG
  107.     d_dbglog ("d_numparse", "%d: (speed %d) num '%s'",
  108.             num, numtab[num].t_speed, numtab[num].t_number);
  109. #endif D_DBGLOG
  110.         num++;
  111.  
  112.         continue;
  113.         }
  114.  
  115. /*  if the string to be parsed contains a '|', then it is supposed to  */
  116. /*  have a preceeding speed indicator.  get at most 4 digits for this  */
  117.  
  118.       if (d_instring('|', tempbuf))
  119.         {
  120.         ap = accum;
  121.         count = 0;
  122.  
  123.         while ((*cp != '|') && (++count <= 4))
  124.           if (isdigit(*cp))
  125.             *ap++ = *cp++;
  126.  
  127.           else
  128.             {
  129.             d_fillog(filename, lineno, "d_numparse", "bad character in speed");
  130.             return(D_FATAL);
  131.             }
  132.  
  133.         if (count > 4)
  134.           {
  135.           d_fillog(filename, lineno, "d_numparse", "invalid speed");
  136.           return(D_FATAL);
  137.           }
  138.  
  139. /*  now look up the string to get the index  */
  140.  
  141.         *ap = '\0';
  142.  
  143.         if ((index = d_spdindex(accum)) < 0)
  144.           {
  145.           d_fillog(filename, lineno, "d_numparse", "unknown speed");
  146.           return(D_FATAL);
  147.           }
  148.  
  149.         numtab[num].t_speed = index;
  150.         cp++;
  151.         }
  152.  
  153. /*  if there's no speed given, assume a default of 300 baud  */
  154.  
  155.       else
  156.         numtab[num].t_speed = 7;
  157.  
  158. /*  accumulate up to 25 characters of phone number  */
  159.  
  160.       ap = numtab[num].t_number;
  161.       count = 0;
  162.       linetype = 0;
  163.  
  164.       while (*cp && (*cp != ',') && (count < 25))
  165.         {
  166.  
  167.         if (isdigit(*cp) || (linetype && (*cp != '>')) )
  168.           {
  169.           *ap++ = *cp++;
  170.           count++;
  171.           continue;
  172.           }
  173.  
  174.         switch (*cp)
  175.           {
  176.           case '(':
  177.           case ')':
  178.           case '-':
  179.  
  180.               cp++;
  181.               continue;
  182.  
  183.           case ':':
  184.  
  185.           *ap++ = '=';      /* wait for next dial tone */
  186.               count++;
  187.               cp++;
  188.               continue;
  189.  
  190.       case '<':
  191.           *ap++ = *cp++;
  192.           count++;
  193.               linetype = 1;
  194.               continue;
  195.  
  196.       case '>':
  197.           *ap++ = *cp++;
  198.           count++;
  199.           linetype = 0;
  200.               continue;
  201.  
  202.       default:              /* may be wrong, but let it pass  */
  203.  
  204.               d_fillog(filename, lineno, "d_numparse",
  205.           "illegal dial character '%c' (%3o'O) in phone number?",
  206.                   *cp, *cp);
  207.               count++;
  208.               cp++;
  209.               continue;
  210.  
  211.           }
  212.         }
  213.  
  214. /*  make sure the number isn't too long  */
  215.  
  216.       if (count >= 25)
  217.         {
  218.         d_fillog(filename, lineno, "d_numparse", "phone number too long");
  219.         return(D_FATAL);
  220.         }
  221.  
  222. /*  make sure the <> were balanced */
  223.  
  224.       if (linetype)
  225.         {
  226.         d_fillog(filename, lineno, "d_numparse", "Line type field invalid");
  227.         return(D_FATAL);
  228.         }
  229.  
  230.       *ap = '\0';
  231.  
  232. #ifdef D_DBGLOG
  233.       d_dbglog ("d_numparse", "%d: (speed %d) num '%s'",
  234.           num, numtab[num].t_speed, numtab[num].t_number);
  235. #endif D_DBGLOG
  236.       num++;
  237.       }
  238.     }
  239.  
  240. /*
  241.  *     D_SPDINDEX
  242.  *
  243.  *     this routine looks up the speed designation string in the table
  244.  *     of legal identifiers and returns the speed index.
  245.  *
  246.  *     speed -- pointer to the speed string
  247.  */
  248.  
  249. d_spdindex(speed)
  250.   register char  *speed;
  251.     {
  252.     extern struct speedtab  d_spdtab[];
  253.     extern int  d_errno;
  254.     register struct speedtab  *sp;
  255.  
  256.     sp = d_spdtab;
  257.  
  258.     while (sp -> s_speed)
  259.       {
  260.       if (strcmp(sp -> s_speed, speed) == 0)
  261.         return(sp -> s_index);
  262.  
  263.       sp++;
  264.       }
  265.  
  266.     d_errno = D_BADSPD;
  267.     return(D_FATAL);
  268.     }
  269.  
  270. /*
  271.  *     D_INSTRING
  272.  *
  273.  *     routine which returns a non-zero value if the specified character
  274.  *     is in the string.
  275.  *
  276.  *     c -- the character to be searched for
  277.  *
  278.  *     string -- the string to search
  279.  */
  280.  
  281. d_instring(c, string)
  282.   char  c;
  283.   register char  *string;
  284.     {
  285.  
  286.     while (*string)
  287.       if (c == *string++)
  288.         return(1);
  289.  
  290.     return(0);
  291.     }
  292.